<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">存放地址</label>
        <el-select
          v-model="query.labId"
          clearable
          style="width: 185px"
          placeholder="存放地址"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in labs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <label class="el-form-item-label">入库时间</label>
        <date-range-picker v-model="query.createdAt" class="date-item" />
        <!-- <label class="el-form-item-label">状态</label>
        <el-input v-model="query.state" clearable placeholder="状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px" />
          </el-form-item>
          <!-- <el-form-item label="类型" prop="type">
            未设置字典，请手动设置 Radio
          </el-form-item> -->
          <el-form-item label="存放地址" prop="labId">
            <el-select
              v-model="form.labId"
              style="width: 370px"
              clearable
              filterable
              :remote-method="getLabs"
              :loading="searchLoading"
              placeholder="请选择存放地址"
            >
              <el-option
                v-for="item in labs"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="数量" prop="amount">
            <el-input v-model="form.amount" style="width: 370px;" />
          </el-form-item> -->
          <!-- <el-form-item label="状态" prop="state">
            未设置字典，请手动设置 Radio
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称" align="center" />
        <el-table-column label="条码" prop="barCode" align="center">
          <template slot-scope="scope">
            <el-image
              :src="
                baseUrl +
                '/api/barCode/showBarCode?id=' +
                scope.row.id +
                '&type=' +
                scope.row.type
              "
              style="height: 40px; vertical-align: middle"
            />
          </template>
        </el-table-column>
        <el-table-column label="二维码" prop="QRCode" align="center">
          <template slot-scope="scope">
            <el-image
              :src="
                baseUrl +
                '/api/barCode/showQRCode?id=' +
                scope.row.id +
                '&type=' +
                scope.row.type
              "
              style="height: 40px; vertical-align: middle"
            />
          </template>
        </el-table-column>
        <el-table-column prop="labName" label="存放地址" align="center" />
        <el-table-column prop="amount" label="数量" align="center" />
        <el-table-column prop="createdAt" label="入库时间" align="center" />
        <!-- <el-table-column prop="state" label="状态" /> -->
        <el-table-column prop="print" label="条形码" align="center">
          <template slot-scope="scope">
            <button
              class="el-button el-button--primary el-button--mini"
              @click="fileDownload(scope.row)"
            >
              <i class="el-icon-printer" size="100" />
            </button>
          </template>
        </el-table-column>
         <el-table-column prop="print" label="二维码" align="center">
          <template slot-scope="scope">
            <button
              class="el-button el-button--primary el-button--mini"
              @click="fileDownloadQR(scope.row)"
            >
              <i class="el-icon-printer" size="100" />
            </button>
          </template>
        </el-table-column>
        <el-table-column
          v-if="checkPer(['admin', 'barCode:edit', 'barCode:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudBarCode from "@/api/barCode";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import { get } from "@/api/lab";
import { printCode } from "@/api/barCode";
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = {
  id: null,
  name: null,
  barCode: null,
  type: 4,
  labId: null,
  createdAt: null,
  updatedAt: null,
  isDeleted: null,
  amount: 1,
  state: 0,
};
export default {
  name: "BarCode",
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "杂物",
      url: "api/barCode",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudBarCode },
      query: {
        type: 4,
      },
      optShow: {
        add: true,
        edit: false,
        del: true,
        download: true,
        reset: true,
        printZW: true,
        printQRZW: true,
      },
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "barCode:add"],
        edit: ["admin", "barCode:edit"],
        del: ["admin", "barCode:del"],
      },
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }],
        labId: [
          { required: true, message: "存放地址不能为空", trigger: "change" },
        ],
        amount: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        state: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
      queryTypeOptions: [
        { key: "name", display_name: "名称" },
        { key: "type", display_name: "类型" },
        { key: "labId", display_name: "存放地址" },
        { key: "state", display_name: "状态" },
      ],
      searchLoading: false,
      labs: [],
      startTime:'',
      endTime: '',
      baseUrl: process.env.VUE_APP_BASE_API,
      searchForm: {
        page: 1,
        size: 1000,
        name: "",
        id: "",
      },
    };
  },
  created() {
    this.getLabs();
    // this.showBarCode()
  },
  methods: {
    getLabs(query = "") {
      this.searchForm.name = query;
      this.searchLoading = true;
      get()
        .then((response) => {
          this.searchLoading = false;
          this.labs = response;
        })
        .catch((_) => {
          this.searchLoading = false;
        });
    },
    fileDownload(row) {
      printCode({
        itemId: row.id,
        itemType: 4,
        codeType: 1
      }).then(res => {
        this.$message.success("已添加到打印队列");
      })
    },
     fileDownloadQR(row) {
      printCode({
        itemId: row.id,
        itemType: 4,
        codeType: 2
      }).then(res => {
        this.$message.success("已添加到打印队列");
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
  },
};
</script>

<style scoped></style>
